import Router from 'koa-router';
const router = new Router();
import query from '../utils/query';
router.prefix('/users');
// 获取全部数据
const foo = async (ctx:Router.RouterContext, next:any) => {
    let {name,state,currentPage4,pageSize4}:any=ctx.request.body;
    let start=(currentPage4-1)*pageSize4;
    // 1=1是为了字符串拼接的时候name中的and然后state中的and相互独立，都能搜索出结果
    let sql=`select * from client where 1 = 1`
    if(name)sql+=` and name like '%${name}%'` 
    if(state)sql+=` and state like '%${state}%'` ;
    let arr= await query(sql)
    let total=arr.length;
    sql+=` limit ${start},${pageSize4}`
    let data=await query(sql)
    ctx.body = {
        data,total
    }
};
router.post('/foo', foo);
// 删除
const bar = async (ctx:Router.RouterContext, next:any) => {
    let {id}:any=ctx.request.body;
    let sql=`delete from client where id=${id}`;
    let data=await query(sql);
    let total=data.length
    ctx.body = {
        data,total
    }
};
router.post('/bar', bar);
// 添加
const add = async (ctx:Router.RouterContext, next:any) => {
    let {name,state,star,lastlink}:any=ctx.request.body;
    let sql=`insert into client(name,state,star,linkman,phone,lastlink,days) values('${name}','${state}',${star},'赵小敏','19238234','${lastlink}','54')`;
    let data=await query(sql);
    let total=data.length
    ctx.body = {
        data,total
    }
};
router.post('/add', add);
// 修改
const upd = async (ctx:Router.RouterContext, next:any) => {
    let {id,name,state,star,lastlink}:any=ctx.request.body;
    let sql=`update client set name='${name}',state='${state}',star=${star},lastlink='${lastlink}' where id=${id}`;
    let data=await query(sql);
    let total=data.length
    ctx.body = {
        data,total
    }
};
router.post('/upd', upd);
export default router;
